package com.oig.sys.base.controller;


import com.oig.common.module.SysResponse;
import com.oig.common.validator.GroupAdd;
import com.oig.common.validator.GroupEdit;
import com.oig.sys.base.dto.ParamDto;
import com.oig.sys.base.service.ITbParamService;
import com.oig.sys.base.vo.ParamVo;
import com.oig.sys.log.annotation.SysLog;
import com.oig.sys.security.util.SecurityUtil;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Tag(name = "系统参数管理")
@RestController
@RequestMapping("/param")
@Slf4j
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class TbParamController {

    private final ITbParamService paramService;

    @Autowired
    public TbParamController(ITbParamService paramService){
        this.paramService = paramService;
    }

    @PreAuthorize("@pms.hasPermission('/param/addParam')")
    @SysLog(value="添加系统参数",apiUrl="/param/addParam")
    @Operation(summary = "添加系统参数")
    @PostMapping(value = "/addParam")
    public ParamVo addParam(@RequestBody @Validated(GroupAdd.class) ParamDto paramDto){
        log.info("add Param {}",paramDto);
        paramDto.setUpdateTime(new Date());
        paramDto.setUpdateUser(SecurityUtil.getDefineUserMain().getRealName());
        return this.paramService.addTbParam(paramDto);
    }

    @PreAuthorize("@pms.hasPermission('/param/updateParam')")
    @SysLog(value="修改系统参数",apiUrl="/param/updateParam")
    @Operation(summary = "修改系统参数")
    @PostMapping(value = "/updateParam")
    public ParamVo updateParam(@RequestBody @Validated(GroupEdit.class) ParamDto paramDto){
        log.info("update Param {}",paramDto);
        paramDto.setUpdateTime(new Date());
        paramDto.setUpdateUser(SecurityUtil.getDefineUserMain().getRealName());
        return this.paramService.updateTbParam(paramDto);
    }

    @PreAuthorize("@pms.hasPermission('/param/cancelParam')")
    @SysLog(value="逻辑删除系统参数",apiUrl="/param/cancelParam")
    @Operation(summary = "逻辑删除系统参数")
    @PostMapping(value = "/cancelParam/{code}")
    public SysResponse<String> cancelParam(@PathVariable("code") String code){
        log.info("cancel Param code={}", code) ;
        this.paramService.cancelTbParam(code);
        return SysResponse.ok(code,"逻辑删除系统参数成功");
    }


    @Operation(summary = "获取参数信息")
    @GetMapping(value = "/getParam/{code}")
    public ParamVo getParam(@PathVariable("code") String code){
        log.info("get Param code={}", code) ;
        return this.paramService.getParam(code);
    }


    @Hidden
    @GetMapping(value = "/getNextId/{code}")
    public Integer getNextId(@PathVariable("code") String code){
        log.info("get NextId code={}", code) ;
        return this.paramService.getNextId(code);
    }


    @Hidden
    //@PreAuthorize("@pms.hasPermission('/param/reset')")
    @SysLog(value="重置系统参数",apiUrl="/param/reset")
    @PostMapping(value = "/reset")
    public void resetAllIdIndex(){
        log.info(" id index reset ") ;
        this.paramService.resetAllIdIndex();
    }


}
